热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

CorePython|2CorePython:GettingStarted|2.5Modularity|2.5.4__name__

Withsomebackgroundonfunctionsinplaceandtheterminallogicalexactitudeestablished,let'sg

With some background on functions in place and the terminal logical exactitude established, let's get back to organizing the code in our words module into function.
We'll move all the code except the import statement, into a function called fetch words. You do that simply by adding the def statement and indenting the code below it by one extra level. Save the module, start a fresh Python REPL, and import your module again with import words. The module imports, but now the words are not fetched until we call the fetch_words function with words.fetch_words. This use of the dot is called qualifying the function name with the module name. Alternatively, we can import are specific function using a different form of the import statement, from words import fetch_words. Having imported the fetch_words function directly into our REPL session, which is itself a module, we can invoke fetch_words using its unqualified name. So far, so good.
But what happens when we try to run our module directly from the operating system shell? Exit from the REPL with Ctrl+D from Mac or Linux, or Ctrl+Z for Windows, and run Python, passing the module file name. No words are printed, that's because all the module does now is define a function and then exit. The function is never called.

 

What we'd prefer is that the module actually print something when we execute it. To make a module from which we can usefully import functions into the REPL and which can be run as a script, we need to learn a new Python idiom. As we mentioned earlier, one of the specially named variables is called dunder name, and it gives us the means to detect whether our module has been run as a script or imported into another module or the REPL. To see how, add print dunder name at the end of your module, outside of the fetch_words function. Let's import the modified words module back into the REPL with import words. We can see that when imported, dunder name does indeed evaluate to the module's name. As a brief aside, if you import the module again in the same REPL, the print statement will not be executed. Module code is only executed once on first import. Now let's try running the module as a script from the operating system shell with Python 3 words.py. Now the special dunder name variable is equal to the string dunder main, which is also delimited by double underscores. That is, Python sets the value of dunder name differently, depending on how our module is being used. The key idea we're introducing here is that our module can use this behavior to decide how it should behave. We replaced the print statement with an if statement, which tests the value of dunder name. If dunder name is equal to the string dunder main, we execute our function. On the other hand, if dunder name is not equal to dunder main, the module knows it's being imported into another module, not executed, and so only defines the fetch_words function without executing it. We can now safely import our module without unduly executing our function, and we can usefully run our module as a script.

 

 

 

于是,我们可以用这个知识来限定某些代码的执行,如果是直接运行其所在的py文件,则代码执行,如果是其他py文件导入当前py文件并运行,则代码不执行

 



推荐阅读
  • Go Cobra命令行工具入门教程
    本文介绍了Go语言实现的命令行工具Cobra的基本概念、安装方法和入门实践。Cobra被广泛应用于各种项目中,如Kubernetes、Hugo和Github CLI等。通过使用Cobra,我们可以快速创建命令行工具,适用于写测试脚本和各种服务的Admin CLI。文章还通过一个简单的demo演示了Cobra的使用方法。 ... [详细]
  • 本文由编程笔记#小编为大家整理,主要介绍了logistic回归(线性和非线性)相关的知识,包括线性logistic回归的代码和数据集的分布情况。希望对你有一定的参考价值。 ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • 本文讨论了在Windows 8上安装gvim中插件时出现的错误加载问题。作者将EasyMotion插件放在了正确的位置,但加载时却出现了错误。作者提供了下载链接和之前放置插件的位置,并列出了出现的错误信息。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • sklearn数据集库中的常用数据集类型介绍
    本文介绍了sklearn数据集库中常用的数据集类型,包括玩具数据集和样本生成器。其中详细介绍了波士顿房价数据集,包含了波士顿506处房屋的13种不同特征以及房屋价格,适用于回归任务。 ... [详细]
  • 本文介绍了三种方法来实现在Win7系统中显示桌面的快捷方式,包括使用任务栏快速启动栏、运行命令和自己创建快捷方式的方法。具体操作步骤详细说明,并提供了保存图标的路径,方便以后使用。 ... [详细]
  • 提升Python编程效率的十点建议
    本文介绍了提升Python编程效率的十点建议,包括不使用分号、选择合适的代码编辑器、遵循Python代码规范等。这些建议可以帮助开发者节省时间,提高编程效率。同时,还提供了相关参考链接供读者深入学习。 ... [详细]
  • 微软头条实习生分享深度学习自学指南
    本文介绍了一位微软头条实习生自学深度学习的经验分享,包括学习资源推荐、重要基础知识的学习要点等。作者强调了学好Python和数学基础的重要性,并提供了一些建议。 ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • 展开全部下面的代码是创建一个立方体Thisexamplescreatesanddisplaysasimplebox.#Thefirstlineloadstheinit_disp ... [详细]
  • 不同优化算法的比较分析及实验验证
    本文介绍了神经网络优化中常用的优化方法,包括学习率调整和梯度估计修正,并通过实验验证了不同优化算法的效果。实验结果表明,Adam算法在综合考虑学习率调整和梯度估计修正方面表现较好。该研究对于优化神经网络的训练过程具有指导意义。 ... [详细]
author-avatar
manassatromble
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有